package com.yy.controller.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.context.BaseContext;
import com.yy.controller.mapper.AdminUserMapper;
import com.yy.controller.service.AdminUserService;
import com.yy.dto.AdminUserDTO;
import com.yy.dto.AdminUserLoginDTO;
import com.yy.entity.AdminUser;
import com.yy.exception.BaseException;
import com.yy.properties.JwtProperties;
import com.yy.result.PageResult;
import com.yy.utils.JwtUtil;
import com.yy.vo.AdminUserLoginVO;
import com.yy.dto.AdminUserPageQueryDTO;
import com.yy.vo.AdminUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 账号登录
     *
     * @param adminUserLoginDTO
     * @return
     */
    @Override
    public AdminUserLoginVO login(AdminUserLoginDTO adminUserLoginDTO) {
        AdminUser adminUser = adminUserMapper.getByUsername(adminUserLoginDTO);
        //1.判断账号是否存在
        if (adminUser == null) {
            //账号不存在 抛出异常
            throw new BaseException("账号不存在!");
        }
        //2.判断密码是否正确
        String password = adminUserLoginDTO.getPassword();
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(adminUser.getPassword())) {
            throw new BaseException("密码错误!");
        }
        //
        if (adminUser.getStatus() == 0) {
            throw new BaseException("账号已被禁用,请联系管理员!");
        }
        //3.创建VO,封装VO,返回VO
        AdminUserLoginVO adminUserLoginVO = new AdminUserLoginVO();
        BeanUtils.copyProperties(adminUser, adminUserLoginVO);
        //获取token
        Map<String, Object> clamins = new HashMap<>();
        clamins.put("adminUserId", adminUserLoginVO.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), clamins);
        adminUserLoginVO.setToken(token);
        return adminUserLoginVO;
    }

    /**
     * 分页查询员工账号
     *
     * @param adminUserPageQueryDTO
     * @return
     */
    @Override
    public PageResult getUserPageList(AdminUserPageQueryDTO adminUserPageQueryDTO) {
        //开始分页查询
        PageHelper.startPage(adminUserPageQueryDTO.getPage(), adminUserPageQueryDTO.getPageSize());

        Page<AdminUserVO> page = adminUserMapper.getPageList(adminUserPageQueryDTO);
        System.out.println("分页查询:"+page);
        long total = page.getTotal();
        List<AdminUserVO>  records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 新增用户
     * @param adminUserDTO
     */
    @Override
    public void addUser(AdminUserDTO adminUserDTO) {
        //验证身份 获取当前进行操作的用户的id
        Long userId = BaseContext.getCurrentId();
        AdminUser user= adminUserMapper.getById(userId);
        //判断当前用户是否为管理员
        Integer permissions = user.getPermissions();
        if (permissions!= 1){
            throw  new BaseException("您不是管理员,没有该权限!");
        }

        //将AdminUserDTO封装到AdminUser
        AdminUser adminUser = new AdminUser();
        BeanUtils.copyProperties(adminUserDTO,adminUser);
        //密码加密
        adminUser.setPassword( DigestUtils.md5DigestAsHex(adminUser.getPassword().getBytes()));
        //添加其他信息
        adminUser.setPermissions(0);
        adminUser.setCreateTime(LocalDateTime.now());
        adminUser.setUpdateTime(LocalDateTime.now());
        adminUser.setCreateUser(userId);
        adminUser.setUpdateUser(userId);
        System.out.println("eeee"+adminUser);
        adminUserMapper.insert(adminUser);
    }

    /**
     * 根据id删除账号
     * @param id
     */
    @Override
    public void deleteUser(Long id) {
        adminUserMapper.delete(id);
    }

    /**
     * 根据id修改账号状态
     * @param id
     */
    @Override
    public void editeStatus(Long id, Integer status) {
        if (status==1){
            status=0;
        }else if (status==0){
            status=1;
        }else {
            throw  new BaseException("该账号状态异常!");
        }
        //获取当前登录账号id
        Long userId = BaseContext.getCurrentId();
        Long updateUser=userId;
        LocalDateTime updateTime=LocalDateTime.now();
        adminUserMapper.editeStatus(id,status,updateUser,updateTime);
    }

    /**
     * 重置密码
     * @param id
     */
    @Override
    public void resetPwd(Long id) {
        String password="123456";
        //加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //获取当前登录账号id
        Long userId = BaseContext.getCurrentId();
        Long updateUser=userId;
        LocalDateTime updateTime=LocalDateTime.now();
        adminUserMapper.resetPwd(id,password,updateUser,updateTime);
    }

    /**
     * 根据用户名修改密码
     * @param adminUserDTO
     */
    @Override
    public void updatePassword(AdminUserDTO adminUserDTO) {
        String password= adminUserDTO.getPassword();
        //加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //获取当前登录账号id
        Long userId = BaseContext.getCurrentId();
        Long updateUser=userId;
        LocalDateTime updateTime=LocalDateTime.now();

        AdminUser adminUser=new AdminUser();
        adminUser.setUsername(adminUserDTO.getUsername());
        adminUser.setPassword(password);
        adminUser.setUpdateUser(updateUser);
        adminUser.setUpdateTime(updateTime);

        adminUserMapper.updatePassword(adminUser);
    }
}

